import { returnBook } from "../../apis/gyt"
import {getBarcode} from "../../utils/block"
var nfc=require("../../utils/nfc");
console.log(nfc);
var _that;
Page({
  data: {switchModeValue:true,bookId:"", result:[] },
  async nfcRead(res){
    var uid=res;
    await nfc.connect();
    var block=await nfc.getBlock(0,3);
    if(block[1]==0){
      wx.showToast({title: '未加工标签',icon:"error"})
      return;
    }
    var barcode=getBarcode(block);
    await nfc.setAfi(0x07);
    await _that.returnBook(barcode,uid);
  },switchMode(){
    var flag=this.data.switchModeValue;
    this.setData({switchModeValue:!flag})
  },
  onLoad(){
    _that=this;
    nfc.start();
    nfc.onread(this.nfcRead);
    var code=getBarcode(new Uint8Array([0,0xE1,4,8,48,48,56,54,56,56,56,56,56]));
    console.log(code);
  },
  onUnload(){
    nfc.unread();
    nfc.close();
    nfc.stop();
  },
 async returnBook(barcode,uid){
     var res=await returnBook(barcode,uid);
     var result=this.data.result;
     if(!res.biblio){
       return wx.showToast({title: '没有图书条码：'+ barcode})
     }
     result.push(res);
     this.setData({result:result});
  },

  scanBarcode() {
    wx.scanCode({
      success: (res) => this.returnBook(res.result)
    })
  },
  
  inputBookId(e) { this.setData({ bookId: e.detail.value }) },
  
  goToConfirm() {
    debugger;
    this.returnBook(this.data.bookId);
  }
})